<?php

namespace App\Http\Services;

use App\Repositories\WechatRepository;

use App\Models\Member;
use App\Models\MessageTemplate;
use App\Models\Config;
use App\Models\Msg;
use Exception;
use Illuminate\Support\Facades\Log;

/**
 * 微信服务类型
 * @package App\Http\Services
 */
class WechatService
{

    private $wechatRep = NULL;
    const COLOR = "#EF6243";

    public function __construct()
    {
        $options = array(
            "app_id" => Config::getValue("weixin_appid"),
            "app_secret" => Config::getValue("weixin_appsecret")
        );
        $this->wechatRep = new WechatRepository($options);
    }

    /**
     * 生成消息内容
     * @Author   thomashtao               bosslen
     * @DateTime 2022-04-08T11:06:00+0800
     * @param    [type]                   $data   [description]
     * @return   [type]                           [description]
     */
    private function _create_template_msg($data, $msg_type)
    {
        $messages = [
            1 => [ //资料审核结果通知
                "亲爱的" . $data['username'] . "，你填写的资料已提交审核。",
                $data['username'],
                date('Y-m-d H:i:s'),
                "正常情况下会在12小时内完成审核，请耐心等待。在这之前你可以检查修改所填写的资料：微信号审核通过之后不允许修改，请确保微信号填写正确且可以被搜索到，安全起见，微信号请勿直接填手机号；请提交本科毕业证或本科学信档案审核，本科在校生可以提交本科学生证，提交硕博阶段的证件将无法通过审核。"
            ],
            2 => [
                "恭喜你互选成功，赶快点击进入【" . $data['activity_title'] . "】互选成功页面查看吧！互选成功添加好友请备注活动信息和联谊编号！",
                $data['toUid'] . "号" . $data['toUsername'],
                $data['fromUid'] . "号" . $data['fromUsername'],
                date('Y-m-d H:i:s'),
                "若互选成功页面没有该嘉宾信息，说明对方已取消选择！请检查个人资料里填的微信号能否被搜索！免责声明：本硕博联谊平台不保证所有用户的所有信息资料的完全真实性，互选成功请进一步互相验证身份学历婚姻状况等信息的真实性，由此引发的任何法律纠纷，本硕博联谊平台概不负责!"
            ],
            3 => [ //移入心动区
                $data['uid'] . "号" . "将你移入心动区啦！",
                $data['username'],
                date('Y-m-d H:i:s'),
                "点击进入【" . $data['activity_title'] . "】被选区页面查看吧！若被选区没有说明已互选成功或对方已经取消了选择！"
            ],
            4 => [ // 互选池-互选成功
                "恭喜你互选成功，点击进入" . $data['elect_pool_title'] . "互选池互选成功页面查看吧！扣账户余额" . $data['price'] . "联谊币。当前账户余额为【" . $data['balance'] . "】。若互选成功页面没有该嘉宾信息，说明Ta已取消选择！联系方式互相可见。",
                $data['toUid'] . "号" . $data['toUsername'],
                $data['fromUid'] . "号" . $data['fromUsername'],
                date('Y-m-d H:i:s'),
                "不保证资料的完全真实性，交往过程中请注意互相验证。注意保留截图等证据！"
            ],
            5 => [//互选池-移入心动区
                $data['uid'] . "号" . "将你移入心动区啦，点击进入" . $data['elect_pool_title'] . "互选池被选区查看吧！若被选区没有说明已互选成功或对方已经取消了选择！",
                $data['username'],
                date('Y-m-d H:i:s'),
                "若将对方也移入心动区即互选成功！头像或生活照是真人但非本人的被投诉要扣账户余额！警惕交友背后的投资理财博彩诈骗【杀猪盘】，回复“骗子”了解骗子套路!"
            ],
            6 => [ //应征-需要审核信息
                "你应征的嘉宾已经查看了您的信息：",
                $data['uid'] . "号" . $data['username'],
                "Yeah，嘉宾通过了您的应征请求~",
                date('Y-m-d H:i:s'),
                "快登陆网站查看TA的联系方式吧！",
            ],
            7 => [ //应征结果提醒 需要审核信息
                "您应征的嘉宾已经查看了您的信息：",
                $data['uid'] . "号" . $data['username'],
                "Oops，很遗憾，嘉宾拒绝了您的应征请求！应征费用已退还一半至你的账户余额，当前余额：" . $data['account'],
                date('Y-m-d H:i:s'),
                "理由：" . $data['select_reason'] . '  ' . $data['reason'],
            ],
            8 => [
                "你有一位新的应征者待接受，点击查看。请勿必在7天内主动接受或者拒绝应征。7天内不处理，应征将自动拒绝。",
                $data['uid'] . "号" . $data['username'],
                "若已脱单或暂时不想被打扰，请务必在服务号对方话框回复【关闭应征】！",
                "无",
                "接受应征微信号互相可见，拒绝互不可见。！"
            ],
            9 => [
                '申请已提交，我们可能在未来7天内发布，若7天内未发布则不再发布。公众号发帖将不区分性别送达所有粉丝。【为了避免浪费工作人员时间，一旦发布，3天之内不允许删除！】',
                '申请【' . $data['name'] . '】公众号推送你的交友资料',
                date('Y-m-d H:i:s'),
                "若需取消务必于推文发布前点本通知进入取消页面！ "
            ],
            10 => [
                $data['uid'] . "号" . $data['username'] . "已经解锁了联系方式，点击查看。互选成功的情况下任何一方发起解锁费用需要双方共摊，本次解锁由对方发起，你需要分摊4元，账户余额已扣除4元，【当前账户余额：" . $data['df_uid'] . "】。账户余额为正下次支付可以抵扣，为负需要充值。若你的微信二维码扫不了且微信号也搜不到引发对方投诉本次解锁费用全部由你承担，对方分摊的解锁费用将你的账户余额扣除并返还给对方。",
                "互选池互选成功解锁联系方式",
                date("Y-m-d H:i:s"),
                ""
            ],
            11 => [
                "你已发帖成功，仅异性可见，自己看不到！请及时接受或拒绝应征，48小时未处理应征请求将自动接受应征。每次发帖自动接受应征次数达到5次系统将取消你的发帖且不退还发帖费用。发帖成功的同时系统也为你打开二维码名片个人资料访问开关，若不需要可以在个人中心设置分享里关闭。",
                '申请发帖',
                date("Y-m-d H:i:s"),
                "点击通知进入取消发帖页面，发帖后“申请发帖”按钮已变为“取消发帖”，在发帖到期之前你可以随时主动取消发帖，脱单后请及时取消发帖！"
            ],
            12 => [
                "恭喜你报名成功，你的联谊编号为【" . $data["uid"] . "】，活动现场要用到，请牢记！",
                $data['title'],
                date('m月d日H:i', strtotime($data["start_time"])) . '~' . date('m月d日H:i', strtotime($data["end_time"])),
                $data["address"],
                '点下方“详情”进入“我的活动”页面，互选、取消报名、查看活动安排。活动地点请以本次活动安排为准。活动计划安排若有变化不一一通知，会第一时间写在活动安排，报名后请密切关注“活动安排”！'
            ],
            13 => [
                '',
                $data['uid'] . "号通过超级心动解锁了你的联系方式。",
                date("Y-m-d H:i:s"),
                "点击进入" . $data['elect_pool_title'] . "互选池被选区查看~",
                ""
            ],
            14 => [
                "邀请奖励到账啦~",
                $data['amount'],
                $data['text'],
                date("Y-m-d H:i:s"),
                "点击继续邀请新用户，获得更多奖励！"
            ],
            15 => [
                $data['uid'] . "号" . $data['username'] . "对你特别有意向，通过附近的人解锁了你的联系方式。点击查看Ta的信息~",
                $data['uid'] . "号" . $data['username'] . "附近的人解锁了你的微信",
                date("Y-m-d H:i:s"),
                "不妨给对方同时也是给自己一个了解的机会吧！"
            ],
            16 => [
                "有人反馈新问题啦！",
                date("Y-m-d H:i:s"),
                $data['content'],
                $data['reply'],
                ''
            ],
            17 => [
                "【警告】用户邀请超出每日阈值，请及时关注！",
                "邀请告警 今日邀请次数：" . $data['count'],
                date("Y-m-d H:i:s"),
                $data['from'] . $data['ruid'],
            ],
            18 => [
                "您提交的头像已经审核了：",
                "头像",
                "未通过",
                "你的头像由于人脸过小（人脸宽度120像素以下）/非真人/真人但非本人/过度侧面/过度遮挡/过度暴露/背景过暗/模糊不清/含有违规信息，未审核通过！请重新上传，否则只能选择其他同样头像未审核通过的嘉宾！",
                "点击去修改！"
            ],
            19 => [
                "说不定Ta就是你要找的人",
                $data['username'],
                "Ta在你的主页停留了许久，快去看看吧！",
                "本提醒可在小程序 我的->我的设置 中关闭",
            ],
            25 => [
                "消息反馈",
                '编号：' . $data['wo_uid'] . ' , ' . date("Y-m-d H:i"),
                $data['content'],
                "点击查看详情",
                "",
            ],
            26 => [
                "",
                '编号：' . $data['uid'] . '-' . $data['username'],
                $data['title'] . ' , ' . date("Y-m-d H:i"),
                $data['content'],
                "点击查看详情",
            ],
            27 => [//发稿通知
                "",
                $data['title'] ?? "",
                $data['content_1'] ?? "",
                $data['content'],
                "点击查看详情",
            ],
            28 => [// 邀请奖励通知
                "",
                $data['price'] ?? "0",
                $data['content'] ?? "邀请奖励佣金",
                date("Y-m-d H:i:s"),
                "",
            ],
            29 => [
                "",
                "实名认证成功~奖励" . $data['idcard_reward'] . "限时联谊币",
                "点击继续完成学历认证奖励" . $data['edu_reward'] . "限时联谊币。",
                date("Y-m-d H:i:s"),
                ""
            ],
            30 => [
                "",
                "学历认证成功~奖励" . $data['edu_reward'] . "限时联谊币",
                "点击申请发帖，最高奖励100永久联谊币。",
                date("Y-m-d H:i:s"),
                ""
            ],
        ];

        return $messages[$msg_type] ?? '';
    }

    /**
     * 公众号消息模板
     * @Author   thomashtao               bosslen
     * @DateTime 2022-04-08T10:52:30+0800
     * @param    [type]                   $openid [description]
     * @param    [type]                   $type   [description]
     * @param    [type]                   $msg_type  [消息内容类型]
     * @param    [type]                   $data   [description] [key => value, msg_type => "", page => ""]
     * @return   [type]                           [description]
     */
    public function sendTemplateMessage($openid, $type, $condition, $msg_type)
    {
        $param = $this->_create_template_msg($condition, $msg_type);
        if (!$param) return false;

        $template_tpls = array(
            1 => array( //资料审核结果通知
                "page" => $condition['page'],
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1],),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3],),
                    'remark' => array('value' => $param[4] ?? ""),
                ),
                'template_id' => 'ZFBVpseyymHoTmVEup73Zpxfr1ISiC9iCnV9ZrkWQyc',
            ),

            2 => array( //报名审核结果通知
                "page" => 'activity/my',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1],),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'keyword4' => array('value' => $param[4]),
                    'remark' => array('value' => $param[5]),
                ),
                'template_id' => 'EBzwfTvJZqtTy_8cb-yiIxFLp8CqNufqvlxs6d_O1BY',
            ),

            3 => array( //应征结果提醒
                "page" => 'message/enlist',
                'data' => array(
                    'first' => array('value' => $param[0],),
                    'keyword1' => array('value' => $param[1],),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => 'v2c_nBcPxOe0mUHBU2N9nKoP5S_cmCpudAgqRCgefm4',
            ),

            4 => array( //新应征者通知
                "page" => 'message/enlistor?current=-1',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => 'aidDe5270VlKC5FYb1_vRZ5dWQwz3CBhoPObs5N93QE',
            ),

            5 => array( //互选成功通知
                "page" => $condition['page'] ?? ("activity/elected?current=3&actId=" . $condition['activity_id']),
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => 'CttvoDsM25WpUWW5_I_74Yk-lGEiTd6pWaoc9O3RdJQ',
            ),

            6 => array( //退出互选池通知
                "page" => 'https://wx.bsbly.com/ElectPoolElected/my_suc',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => 'cgITXKfzBzS2JaI1fcxFhazeg1QV3TDlsLMtQ1xmJtw',
            ),

            7 => array( //业务到期通知  发帖到期提醒
                "page" => $condition['page'],
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'keyword4' => array('value' => $param[4]),
                    'remark' => array('value' => $param[5]),
                ),
                'template_id' => 't7wbylTYIm_mRDQgGEqNUmnskls9yMYJXe1jF2z44r8',
            ),
            8 => array( //客户状态通知，即新发帖提醒
                "page" => 'enlist/index',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => 'Tr6SS_IePvWGlMPq72KFJHpxDZg3inoG_GNEhPENdo4',
            ),
            9 => array( //报名成功通知 活动报名成功
                "page" => 'activity/my',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keynote1' => array('value' => $param[1]),
                    'keynote2' => array('value' => $param[2]),
                    'keynote3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => '7QUONUp0BAF4FhCglOPzBly76bPycfU_q_pKqLb34ik',
            ),
            10 => array( //申请提交成功通知 发帖成功通知
                "page" => 'apply_stage/index',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => '41pxM04M95i4cfk_vcMBxTlXUslEbebDkOLtfXQxLZI',
            ),
            11 => array( //资料提交成功提醒  资料提交审核提醒
                "page" => 'elect-pool/my',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => 'J9Cxv_BStwY7E2CQd1nSMQIUQtOhitehQ3F3P4vCqK4',
            ),
            12 => array( //报名成功通知  活动即将开始提醒 。
                "page" => 'activity/my',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keynote1' => array('value' => $param[1]),
                    'keynote2' => array('value' => $param[2]),
                    'keynote3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => '1xDMUlGpxbrK2A0RA7PqRWmOgzV0ONEDZx7GjhaN7-U',
            ),

            13 => array( //关注成功通知  被选通知
                "page" => $condition['page'] ?? ("activity/elected?current=2&actId=" . $condition['activity_id']),
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => '8CzJLwLhgxl_CIGSrObIH8PvDyEOozI2J3-S6KHv0qM',
            ),

            14 => array( //服务申请通知,公众号不推送通知
                "page" => $condition['page'],
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => '_pjqFi8CaT56XgdX3Jzsy7TRJ45yI2uxjh3pt572Uw0',
            ),

            15 => array( //服务取消通知，多次应征自动通过取消发帖通知
                "page" => 'enlist/index',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => 'ScGqmEzsZ9hezqZOgbF2mnzh_yEN9nGbXKJZsOG303M',
            ),

            16 => array( //投诉处理结果通知
                "page" => '',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => '-m8Z8bwE7s3ZVzZ8pMsp-zSSSydwPVUmLh9Ob9nsLgk',
            ),

            17 => array( //服务完成提醒
                "page" => $condition['page'],
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => 's1sX8VbEl1Q0c5NiZMcqRXmwdyxJMxaW9v-TeTPW-ks',
            ),

            18 => array( //申请推送成功提醒
                "page" => 'apply_stage/index',
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2],),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => 'apmc8cjfRsqL5YrDWOCF0HGBAOTU0X7Q5sGbLi1tI-I',
            ),

            19 => array( //成员加入提醒
                "page" => $condition['page'], //解封关闭
//                "page" => "elect-pool/my",
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3],),
                ),
                'template_id' => 'Z34LQGk3eiazqtjV-4HMaFrBfWrVAFYV91uLIrQfgeM',
            ),

            20 => array( //会话开启成功通知
                "page" => $condition['page'],
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => 'Nuf2JpinfQW_bduEjdnmYOmMnpqPur2_qMVxxZ3Yg3U',
            ),

            21 => array( //积分扣除通知
                "page" => $condition['page'],
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'keyword4' => array('value' => $param[4]),
                    'remark' => array('value' => $param[5]),
                ),
                'template_id' => 'ks3JPOdgis3MtfftE8yUQX2vXaMAmdQlgDsUzC8W38E',
            ),

            22 => array( //收益到账通知 用于小程序推荐新用户奖励到账通知
                "page" => $condition['page'] ?? "share/content",
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => 'yD7Rz5M7RdGNF_uZTQgNmN2Bn3eyjin6NABDNNV9MSk',
            ),

            23 => array( // 访客接待提醒
                "page" => $condition['page'] ?? "member-visit/index",  //解封关闭
//                "page" => "member-visit/index",
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => '5UL7UB0f1eKOqUudrYaSk1ezQaK1K1nXs7OvUI9bT9E',
            ),

            25 => array( // 反馈结果通知
                "page" => $condition['page'] ?? "post/index",
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => 's1sX8VbEl1Q0c5NiZMcqRXmwdyxJMxaW9v-TeTPW-ks',
            ),
            26 => array( // 服务单进度提醒
                "page" => $condition['page'] ?? "post/index",
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => 'WI358FeqFvZkF75lqwBfYyQ3Pa8U7-CK-BeNmU3B141',
            ),
            27 => array( //服务完成提醒
                "page" => $condition['page'],
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'remark' => array('value' => $param[3]),
                ),
                'template_id' => 'h4AGV12OhOX37dmNTvSK5nKpdHMAP2I6xKLUCC6TM5g',
            ),
            28 => array( //超级心动 申请结果通知
                "page" => $condition['page'],
                'data' => array(
                    'first' => array('value' => $param[0]),
                    'keyword1' => array('value' => $param[1]),
                    'keyword2' => array('value' => $param[2]),
                    'keyword3' => array('value' => $param[3]),
                    'remark' => array('value' => $param[4]),
                ),
                'template_id' => '3cgmNOce0c2BhJsvinjgbWeuiUXEo6nROVVe9f4-Qpk',
            ),
        );

        $templateData = $template_tpls[$type];
        try {
            $res = $this->wechatRep->sendTemplateMessage($templateData['template_id'], $openid, $templateData['data'], $templateData['page'] ?? "");

            $member = Member::where("openid", $openid)->first();
            if ($member) {
                $fun = $res['errcode'] == 0 ? 'info' : 'error';

                $log_data = $templateData['data'];
                $log_data['errmsg'] = $res['errmsg'] ?? "";
                $log_data['errcode'] = $res['errcode'] ?? 0;
                $log_data['openid'] = $member['openid'];
                Log::channel("template")->$fun('发送人昵称：' . $member['id'] . ' ' . $member['username'] . ',templateId: ' . $templateData['template_id'] . ',' . ($res['errcode'] ?? 0) . ', params: ' . json_encode($log_data));
            }

            //记录消息
            if (!in_array($type, [23, 22])) {
                $this->handle_msg($type, $condition);
                // Log::notice("send Message success: " . $openid . print_r($res, true));
            }

            //写log
            $new_add['type'] = $type;
            $new_add['openid'] = $openid;
            $new_add['errcode'] = $res['errcode'];
            $new_add['errmsg'] = $res['errmsg'];
            $new_add['template_id'] = $templateData['template_id'];
            $new_add['create_time'] = date('Y-m-d H:i:s');
            $new_add['original'] = 2; //

            $new_add['data'] = json_encode($templateData);
            //暂时去掉
//            MessageTemplate::insert($new_add);

            if ($res['errcode'] == '43004') {// 记录未关注服务号
                Member::where('openid', '=', $openid)->update(['is_subscribe' => 0]);
            }

            return $res;
        } catch (Exception $e) {
            Log::error("send Message error: " . print_r($e, true));
        }

    }

    private function handle_msg($type, $condition = [])
    {
        if (sizeof($condition) == 0) {
            return;
        }
        $add['type'] = $type;
        $add['object_type'] = $condition['object_type'];
        $add['object_id'] = $condition['object_id'];
        $add['uid'] = $condition['wo_uid'];
        $add['df_uid'] = $condition['df_uid'];
        $add['content'] = $condition['content'];
        $add['create_time'] = date("Y-m-d H:i:s");
        if ($type && in_array($type, [1, 2, 9, 10, 12, 18, 21, 22])) {
            $add['from'] = 1;//1：系统消息
            $add['uid'] = 0;
            $add['df_uid'] = $condition['wo_uid'];
        }
        if ($type && in_array($type, [3, 4, 5, 13, 16, 17])) {
            $add['from'] = 2;//2：会员消息
            $add['uid'] = $condition['wo_uid'];
            $add['df_uid'] = $condition['df_uid'];
        }
        Msg::insert($add);
    }

    /**
     * 处理订阅消息
     * @param $type
     * @param $condition
     */
    public function handleSubscribeMsg($type, $condition = [])
    {
        $this->handle_msg($type, $condition);
    }

}
